package com.taxshare.purchaser.util;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.taxshare.purchaser.entity.model.InvoiceDetailModel;
import com.taxshare.purchaser.entity.model.InvoiceMainInfoModel;
import com.taxshare.tools.util.Base64Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InvoiceCheckUtil {

    /**
     * 查验结果
     *
     * @param checkCode
     * @return
     */
    public static String checkResult(String checkCode) {
        switch (checkCode) {
            case "0000":
                return "查验成功发票一致";
            case "CY0001":
                return "发票类型不能为空";
            case "CY0002":
                return "专票未税金额不能为空/车价合计不能为空";
            case "CY0003":
                return "普票后六位校验码不能为空";
            case "CY0004":
                return "普票后六位校验码格式错误";
            case "CY0005":
                return "发票代码格式错误";
            case "CY0006":
                return "发票号码格式错误";
            case "CY0007":
                return "开票日期格式错误";
            case "CY0008":
                return "一年以前开具的发票无法查询";
            case "CY0009":
                return "开票日期格式校验异常";
            case "CY0010":
                return "专票未税金额格式错误/车价合计格式错误";
            case "CY0011":
                return "该张发票超出当天查验次数";
            case "CY0012":
                return "网络异常";
            case "CY0013":
                return "网络超时";
            case "CY0014":
                return "发票查验请求太频繁，请稍后再试";
            case "CY0015":
                return "超过服务器最大请求数，请稍后访问";
            case "CY0016":
                return "请求不合法";
            case "CY0017":
                return "发票数据不一致";
            case "CY0018":
                return "查无此票";
            case "CY0019":
                return "服务器网络异常，未获取的发票信息";
            case "CY0020":
                return "超过服务器最大请求数，请稍后访问";
            case "CY0021":
                return "不存在这样的地区编码";
            case "CY0022":
                return "发票代码不合法";
            case "CY0023":
                return "发票号码不合法";
            case "CY0024":
                return "开票日期不合法";
            case "CY0025":
                return "开票金额或校验码不合法";
            case "CY0026":
                return "当日开具发票可于次日进行查验";
            case "CY0027":
                return "未知异常，请联系管理员";
            case "CY0028":
                return "其他异常";
            case "CY0029":
                return "内部异常，请联系管理员";
            case "CY0030":
                return "该张票超过当天查验次数（税局）";
            case "CY0031":
                return "查验失败，请稍后重试";
            default:
                return "未知异常";
        }
    }

    /**
     * 查验结果
     *
     * @param invoiceStatus
     * @return
     */
    public static String transInvoiceStatus(String invoiceStatus) {
        switch (invoiceStatus) {
            case "Y":
                return "1";
            case "N":
                return "2";
            case "3":
                return "3";
            default:
                return "";
        }
    }


    public static Map<String, Object> assembly(String content) {
        String checkContent = Base64Util.decode(content);
        JSONObject jsonObject = JSONUtil.parseObj(checkContent);
        JSONObject fpxxJsonObject = jsonObject.getJSONObject("fpxx");
        InvoiceMainInfoModel mainInfoModel = JSONUtil.toBean(fpxxJsonObject, InvoiceMainInfoModel.class);
        List<InvoiceDetailModel> detailList = JSONUtil.toList(fpxxJsonObject.getJSONArray("sph"), InvoiceDetailModel.class);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("invoiceMainInfo", mainInfoModel);
        resultMap.put("invoiceDetailInfo", detailList);
        return resultMap;
    }

}
